package com.vanzo.some.action;

import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2017/6/8.
 */

public class HandActionParser implements ActionParse {
    @Override
    public List<HandAction> parse(XmlPullParser parser) throws Exception {
        List<HandAction> actions = null;
        HandAction action = null;
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    actions = new ArrayList<>();
                    break;
                case XmlPullParser.START_TAG:
                    Log.d("START_TAG","name = "+parser.getName());
                    if (parser.getName().equals("handaction")) {
                        action = new HandAction();
                    } else if (parser.getName().equals("x")) {
                        eventType = parser.next();
                        action.setX(Integer.parseInt(parser.getText()));
                    } else if (parser.getName().equals("y")) {
                        eventType = parser.next();
                        action.setY(Integer.parseInt(parser.getText()));
                    } else if (parser.getName().equals("delay")) {
                        eventType = parser.next();
                        action.setDelay(Integer.parseInt(parser.getText()));
                    } else if (parser.getName().equals("action")) {
                        eventType = parser.next();
                        action.setAction(Integer.parseInt(parser.getText()));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("handaction")) {
                        actions.add(action);
                        action = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
        return actions;
    }

    @Override
    public String serialize(List<HandAction> actions) throws Exception {
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8",true);
        serializer.startTag("","handactions");
        for(HandAction action:actions){
            serializer.startTag("","handaction");

            serializer.startTag("","x");
            serializer.text(action.getX()+"");
            serializer.endTag("","x");

            serializer.startTag("","y");
            serializer.text(action.getY()+"");
            serializer.endTag("","y");

            serializer.startTag("","delay");
            serializer.text(action.getDelay()+"");
            serializer.endTag("","delay");

            serializer.startTag("","action");
            serializer.text(action.getAction()+"");
            serializer.endTag("","action");

            serializer.endTag("","handaction");
        }
        serializer.endTag("","handactions");
        serializer.endDocument();
        return writer.toString();
    }
}
